
#include "StatePopUp.h"
#include "StateManager.h"
#include "InputManager.h"
#include "AudioUtils.h"
#include "ScaleEffect.h"

void StatePopUp::loadContent()
{
    m_oFade = new RenderableEntity("fade", Vector2(0, 0));
    m_oFade->setScale(Vector2(640, 960));
    m_oFade->setColor(COLOR_BLACK);
    m_oFade->setAlpha(0);
    
    m_oBg = new RenderableEntity("messagedialog", Vector2(320, 480));
    m_oBg->setScale(Vector2(1.2f, 1.2f));
    m_oBg->setCentered();
    
    m_oText = new TextEntity("PopUp", FONT_NAME, 32, Vector2(320, 500), 1, 1, true, 450);
    m_oText->setCentered();
    //m_oText->setShadow(true, COLOR_WHITE);
    m_oText->setColor(COLOR_BLACK);
    
    m_oLeftBg = new RenderableEntity("buttonyesNO", Vector2(150, 350));
    m_oLeftBg->setScale(Vector2(1.0f, 1.0f));
    m_oLeftBg->setCentered();
    m_oLeftBg->changeColorWhenClicked(true);

    m_oLeft = new TextEntity("Close", FONT_NAME, 36, Vector2(150, 350), 1, 1);
    m_oLeft->setCentered();
    m_oLeft->setShadow(true);

    m_oRightBg = new RenderableEntity("buttonyesNO", Vector2(490, 350));
    m_oRightBg->setScale(Vector2(1.0f, 1.0f));
    m_oRightBg->setCentered();
    m_oRightBg->changeColorWhenClicked(true);

    m_oRight = new TextEntity("Close", FONT_NAME, 36, Vector2(490, 350), 1, 1);
    m_oRight->setCentered();
    m_oRight->setShadow(true);


    m_oEnter = new TimeLineEffect(false, false);
	m_oEnter->addEffect(new FadeEffect(m_oFade, 0.10, false, false, 0.0f, 0.7f), 0);
    
	m_oEnter->addEffect(new ScaleEffect(m_oBg, 0.10, false, false, Vector2(0, 0), Vector2(1.5, 1.5)), 0);
	m_oEnter->addEffect(new ScaleEffect(m_oText, 0.10, false, false, Vector2(0, 0), Vector2(1.2, 1.2)), 0);
	m_oEnter->addEffect(new ScaleEffect(m_oLeftBg, 0.10, false, false, Vector2(0, 0), Vector2(1.2, 1.2)), 0);
	m_oEnter->addEffect(new ScaleEffect(m_oLeft, 0.10, false, false, Vector2(0, 0), Vector2(1.2, 1.2)), 0);
	m_oEnter->addEffect(new ScaleEffect(m_oRightBg, 0.10, false, false, Vector2(0, 0), Vector2(1.2, 1.2)), 0);
	m_oEnter->addEffect(new ScaleEffect(m_oRight, 0.10, false, false, Vector2(0, 0), Vector2(1.2, 1.2)), 0);
	
	m_oEnter->addEffect(new ScaleEffect(m_oBg, 0.05, false, false, Vector2(1.5, 1.5), Vector2(1.2, 1.2)), 0.10);
	m_oEnter->addEffect(new ScaleEffect(m_oText, 0.05, false, false, Vector2(1.2, 1.2), Vector2(1, 1)), 0.10);
	m_oEnter->addEffect(new ScaleEffect(m_oLeftBg, 0.05, false, false, Vector2(1.2, 1.2), Vector2(1, 1)), 0.10);
	m_oEnter->addEffect(new ScaleEffect(m_oLeft, 0.05, false, false, Vector2(1.2, 1.2), Vector2(1, 1)), 0.10);
	m_oEnter->addEffect(new ScaleEffect(m_oRightBg, 0.05, false, false, Vector2(1.2, 1.2), Vector2(1, 1)), 0.10);
	m_oEnter->addEffect(new ScaleEffect(m_oRight, 0.05, false, false, Vector2(1.2, 1.2), Vector2(1, 1)), 0.10);
    
    
    m_oExit = new TimeLineEffect(false, false);
	m_oExit->addEffect(new FadeEffect(m_oFade, 0.10, false, false, 0.7f, 0.0f), 0);
    
	m_oExit->addEffect(new ScaleEffect(m_oBg, 0.05, false, false, Vector2(1.2, 1.2), Vector2(1.5, 1.5)), 0);
	m_oExit->addEffect(new ScaleEffect(m_oText, 0.05, false, false, Vector2(1, 1), Vector2(1.2, 1.2)), 0);
	m_oExit->addEffect(new ScaleEffect(m_oLeftBg, 0.05, false, false, Vector2(1, 1), Vector2(1.2, 1.2)), 0);
	m_oExit->addEffect(new ScaleEffect(m_oLeft, 0.05, false, false, Vector2(1, 1), Vector2(1.2, 1.2)), 0);
	m_oExit->addEffect(new ScaleEffect(m_oRightBg, 0.05, false, false, Vector2(1, 1), Vector2(1.2, 1.2)), 0);
	m_oExit->addEffect(new ScaleEffect(m_oRight, 0.05, false, false, Vector2(1, 1), Vector2(1.2, 1.2)), 0);
    
	m_oExit->addEffect(new ScaleEffect(m_oBg, 0.10, false, false, Vector2(1.5, 1.5), Vector2(0, 0)), 0.05);
	m_oExit->addEffect(new ScaleEffect(m_oText, 0.10, false, false, Vector2(1.2, 1.2), Vector2(0, 0)), 0.05);
	m_oExit->addEffect(new ScaleEffect(m_oLeftBg, 0.10, false, false, Vector2(1.2, 1.2), Vector2(0, 0)), 0.05);
	m_oExit->addEffect(new ScaleEffect(m_oLeft, 0.10, false, false, Vector2(1.2, 1.2), Vector2(0, 0)), 0.05);
	m_oExit->addEffect(new ScaleEffect(m_oRightBg, 0.10, false, false, Vector2(1.2, 1.2), Vector2(0, 0)), 0.05);
	m_oExit->addEffect(new ScaleEffect(m_oRight, 0.10, false, false, Vector2(1.2, 1.2), Vector2(0, 0)), 0.05);
}

void StatePopUp::unloadContent()
{
    delete m_oBg;
    delete m_oLeft;
    delete m_oRight;
    delete m_oText;
}

void StatePopUp::enter()
{
    m_oEnter->start(true);
    m_eState = POPUP_ENTER;
}

void StatePopUp::update(float dt)
{
    switch(m_eState)
    {
        case POPUP_ENTER:
            if(m_oEnter->isFinished())
            {
                m_eState = POPUP_IDLE;
            }
            break;
            
        case POPUP_IDLE:
            if(m_bShowRight)
                m_oRightBg->update();
            
            if(m_bShowLeft)
                m_oLeftBg->update();
                    
            if(m_bShowLeft && m_oLeft->isClicked())
            {
                m_bResult = false;
                m_eState = POPUP_EXIT;
                m_oExit->start(true);
            }
            else if(m_bShowRight && m_oRight->isClicked())
            {
                m_bResult = true;
                m_eState = POPUP_EXIT;
                m_oExit->start(true);
            }
            break;
            
        case POPUP_EXIT:
            if(m_oExit->isFinished())
            {
                StateManager::instance().popState();
            } 
            break;
    }
}

void StatePopUp::render()
{
    m_oFade->render();

    m_oBg->render();
    
    m_oText->render();
    
    if(m_bShowRight)
    {
        m_oRightBg->render();
        m_oRight->render();
    }
    
    if(m_bShowLeft)
    {
        m_oLeftBg->render();
        m_oLeft->render();
    }
}

void StatePopUp::exit()
{
}

void StatePopUp::popupInfo(std::string _sMessage)
{
    popup(_sMessage, true, "Close", false);
}

void StatePopUp::popupConfirm(std::string _sMessage)
{
    popup(_sMessage);
}

void StatePopUp::popup(std::string _sMessage, bool _bShowRight, std::string _sRightText, bool _bShowLeft, std::string _sLeftText)
{
    m_oText->setText(_sMessage);
    
    m_bShowLeft = _bShowLeft;
    if(m_bShowLeft)
    {
        m_oLeft->setText(_sLeftText);
    }
    
    m_bShowRight = _bShowRight;
    if(m_bShowRight)
        m_oRight->setText(_sRightText);
    
    
    StateManager::instance().pushState(instance());
}

bool StatePopUp::getResult()
{
    return m_bResult;
}
